<?php

/**
 * CmppConnectResp.
 *
 * @author  yyc
 * @version 1.0
 * @package main
 * @date 2017-06-08 0008
 * @time 11:08:54
 */

/**
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

namespace cmpp;

use \Exception;
use cmpp\CmppResp;
use cmpp\CmppSend;
class CmppConnectResp extends CmppResp
{
    const status = ['0' => '正确','1' => '消息结构错','2' => '非法源地址','3' => '认证错','4' => '版本太高','5' => '其他错误'];

//    public static function unPack($data){
//        $body = unpack("NStatus/a16Auth/cVersion",$data);
//        $Auth = unpack('H*',$body['Auth']);
//        $body['Auth'] = $Auth['1'];
//        unset($Auth);
//        self::run($body);
//    }

//    public function run($body){
//        if($body['Status'] > 0){
//            $body['Status'] = $body['Status'] > 5 ? 5 :$body['Status'];
//            L('CmppConnectResp')->error(self::status[$body['Status']]);
//            throw new Exception("connection error ".$body['Status']);
//        }
//         swoole_timer_tick($this->config->get('cmpp.sendActivateTime'), function($timer_id)use($cli){
//            if($cli->isConnected()){
//                $msg = CmppSend::testActive();
//                $cli->send($msg);
//            }else{
//                //var_dump($timer_id);
//                swoole_timer_clear($timer_id);
//                $this->run();
//            }
//        });
//        L('CmppConnectResp')->info('connect success');
//    }

    public function connectResp($data,$cli){
        //var_dump($cli);
        $data = self::unPack($data);
        if($data['Status'] > 0){
            $data['Status'] = $data['Status'] > 5 ? 5 :$data['Status'];
            L('CmppConnectResp')->error(self::status[$data['Status']]);
            throw new Exception("connection error ".$data['Status']);
            exit("connection error ".$data['Status']);
        }
        swoole_timer_tick(C()->get('cmpp.sendActivateTime'), function($timer_id)use($cli){
            if($cli->isConnected()){
                $msg = CmppSend::testActive();
                $cli->send($msg);
            }else{
                swoole_timer_clear($timer_id);
                $client = new \server\Client();
                $client->run();
            }
        });

        swoole_timer_tick(1000, function($timer_id)use($cli){
            if($cli->isConnected()){
                $rs = R()->rPop(C()->get('key.down'));

                if($rs) {
                    $rs = json_decode($rs, true);
                    if (isset($rs['phone']) && isset($rs['srcID']) && isset($rs['msg'])) {
                        if(strlen($rs['msg']) <= 140){
                            $msg = CmppSend::shortMsg($rs['phone'],$rs['srcID'],$rs['msg'],$rs['feeCode'],$rs['feeType'],$rs['serviceid'],$rs['delivery'],$rs['linkID']);
                            $cli->send($msg);
                        }else{
                            $msg = CmppSend::longMsg($rs['phone'],$rs['srcID'],$rs['msg'],$rs['feeCode'],$rs['feeType'],$rs['serviceid'],$rs['delivery'],$rs['linkID']);
                            if($msg){
                                foreach ($msg as $m){
                                    $cli->send($m);
                                }
                            }
                        }
                    }
                }
            }else{
                swoole_timer_clear($timer_id);
            }
        });

        L('CmppConnectResp')->info('connect success');
        //var_dump($data);
    }

    public static function unPack($data){
        $body = unpack("NStatus/a16Auth/cVersion",$data);
        $Auth = unpack('H*',$body['Auth']);
        $body['Auth'] = $Auth['1'];
        unset($Auth);
        return $body;
    }




}